package org.compiere.model;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class MFI_C_PaymentLine extends X_FI_C_PaymentLine{	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 9205411059944617172L;
	
	/**	Logger			*/
	private static CLogger s_log = CLogger.getCLogger(MFI_C_PaymentLine.class);

	public MFI_C_PaymentLine(Properties ctx, int FI_C_PaymentLine_ID,
			String trxName) {
		super(ctx, FI_C_PaymentLine_ID, trxName);
		// 
//		setAmount(new BigDecimal(100));
//		setC_BPartner_ID(1000007);
		
	}
	
	/**
	 * @param ctx
	 * @param rs
	 * @param trxName
	 */
	public MFI_C_PaymentLine(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
		// 
	}
	
	public String toString(){
		return getC_BPartner_ID() + ", " + getAmount();
	}
	
	public static List<MFI_C_PaymentLine> getMFI_C_PaymentLines(CLogger log, Properties ctx, Integer PaymentID){
		
		if(PaymentID == null || PaymentID == 0)
			return null;
		
		List<MFI_C_PaymentLine> list = new ArrayList<MFI_C_PaymentLine>();
		
		String sql = "select fi_c_paymentline_id from fi_c_paymentline where c_payment_id = ? ";

		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try
		{
			pstmt = DB.prepareStatement (sql, null);
			pstmt.setInt(1, PaymentID);
			rs = pstmt.executeQuery ();
			while (rs.next ())
			{
				Integer FI_C_PaymentLine_ID = rs.getInt(1);
				MFI_C_PaymentLine line = new MFI_C_PaymentLine(ctx, FI_C_PaymentLine_ID, null);
				list.add(line);
			}
		}
		catch(Exception e){
			log.log (Level.SEVERE, sql, e);
		}
		finally
		{
			DB.close (rs, pstmt);
			rs = null;
			pstmt = null;
		}

		
		return list;
		
	}
	
	protected boolean afterSave (boolean newRecord, boolean success)
	{
		//	Update Payment Header
//		String sql = "UPDATE C_Payment p"
//			+ " SET PayAmt="
//				+ "(SELECT COALESCE(SUM(Amount),0) FROM FI_C_PaymentLine pl WHERE p.C_Payment_ID=pl.C_Payment_ID) "
//			+ "WHERE C_Payment_ID=?";
//		int no = DB.executeUpdateEx(sql, new Object[]{getC_Payment_ID()}, get_TrxName());
//		if (no != 1)
//			log.warning("(1) #" + no);
		
		return true;
	}	//	afterSave
	
	@Override
	protected boolean beforeDelete() {
		
		int C_Payment_ID = get_ID();
		
		BigDecimal PayAmt = new BigDecimal(0);
				
		List<MFI_C_PaymentLine> list = MFI_C_PaymentLine.getMFI_C_PaymentLines(log,  Env.getCtx(), C_Payment_ID);
		for(int i=0; i < list.size(); i++ )
			PayAmt = PayAmt.add(list.get(i).getAmount());
	
		if(PayAmt.compareTo(new BigDecimal(0)) == 1){
			
			String sql = "UPDATE adempiere.C_Payment p"
					+ " SET PayAmt= ? "
					+ " WHERE C_Payment_ID=?";
				int no = DB.executeUpdateEx(sql, new Object[]{PayAmt, C_Payment_ID}, null);
		}		
				
		return super.beforeDelete();
	}
	
	public static MFI_C_PaymentLine getSelect(int C_BPartner_ID, int C_Payment_ID) {
		
		if (C_BPartner_ID == 0)
			return null;
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		MFI_C_PaymentLine result = null;		
									
		// 
		String sql_ = "SELECT * FROM FI_C_PaymentLine \n" +
						" WHERE AD_CLIENT_ID="  + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
							" AND C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
							" AND C_Payment_ID =" + C_Payment_ID;
		try
		{
			pstmt = DB.prepareStatement(sql_,null);
			rs = pstmt.executeQuery();	
			if(rs.next())
				result = new MFI_C_PaymentLine(Env.getCtx(), rs, null);
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}	
			
		return result;
	}
}
